<template>
	<view class="wrap safe-area">
		<view class="wrap-content" v-if="isRequestFinish">
			<block v-for="(item, index) in list" :key="index">
				<view class="wrap-white">
					<block v-for="(subItem, idx) in item" :key="idx">
						<inputRowVue v-if="subItem.type === 'input' && subItem.direct === 'row' && subItem.isShow"
							:item="subItem" v-model="dataInfo[subItem.key]" />

						<pickerRowVue
							v-else-if="subItem.type === 'picker' && subItem.direct === 'row' && subItem.isShow"
							:item="subItem" v-model="dataInfo[subItem.key]" />

						<groupRowVue v-else-if="subItem.type === 'group' && subItem.direct === 'row' && subItem.isShow"
							:item="subItem" v-model="dataInfo[subItem.key]" />

						<uploadColumnVue
							v-else-if="subItem.type === 'upload' && subItem.direct === 'column' && subItem.isShow"
							:item="subItem" v-model="dataInfo[subItem.key]" />

						<skuVue v-else-if="subItem.type === 'sku'" :item="subItem" :spu.sync="dataInfo.spu"
							v-model="dataInfo[subItem.key]" />

						<editorVue v-else-if="subItem.type === 'editor'" v-model="dataInfo[subItem.key]"
							:item="subItem" />

						<wrapTextareaVue v-else-if="subItem.type === 'textarea'" :item="subItem"
							v-model="dataInfo[subItem.key]" />
					</block>
				</view>
			</block>

			<view class="sure-btn" @click="onSubmit">
				{{ goodsID === '' ? "确认发布" : "保存" }}
			</view>
		</view>
	</view>
</template>

<script>
	import {
		GetLogisticsList,
		GetShopGoodsCategory,
		GetGoodsCategory,
		GetBrandList,
		AddGoods,
		EditGoods,
		GetGoodsDetailis
	} from "@/utils/API.js";

	import inputRowVue from "../components/form/inputRow.vue";
	import pickerRowVue from "../components/form/pickerRow.vue";
	import groupRowVue from "../components/form/groupRow.vue";
	import uploadColumnVue from "../components/form/uploadColumn.vue";
	import skuVue from "../components/form/sku.vue";
	import editorVue from "../components/form/editor.vue";
	import wrapTextareaVue from "../components/form/textarea.vue";

	export default {
		components: {
			inputRowVue,
			pickerRowVue,
			groupRowVue,
			uploadColumnVue,
			skuVue,
			editorVue,
			wrapTextareaVue,
		},
		data() {
			return {
				goodsID: "", //商品ID
				dataInfo: {
					title: "", //商品名称
					category_id: "", //商品分类
					shop_category_id: "", //店铺内商品分类
					brand_id: "", //品牌ID
					is_chufang: "0", //是否是处方药 0非 1是
					status: "1", //是否审核后直接上架 1是 0否
					company: "", //生产厂家
					content: "", //详情
					description: "", //商品描述
					commission_method: "1", //分佣方式 1为按金额 2为按比例
					user_commission: "", //用户佣金金额/比例
					vip_commission: "", //店铺会员佣金金额/比例
					price: "",
					stock_num: "",
					goods_yuan_price:"",
					goods_yuan_price:"",
					freight_id: "", //快递模版
					delivery_type: "1", //提货方式
					instructions: "", //说明书
					image: "", //图片
					images: "", //组图

					stock: "", //库存计算方式

					sku: [], //属性
					spu: [], //规格
					xianzhi:'',
					xuzhi:'',
					baozhang:'',
					fuwuxiangqing:'',
					goumaixuzhi:'',
				},
				isRequestFinish: false,

				LogisticsList: [], //运费模版列表
				shopCategoryList: [], //店铺内部分类列表
				categoryList: [], //平台分类列表
				brandList: [], //品牌列表
			}
		},
		computed: {
			list() {
				return [
					[{
							"title": "团购名称",
							"type": "input",
							"direct": "row",
							"key": "title",
							"mode": "text",
							"place": "请输入团购名称",
							"tips": "请输入团购名称",
							"belong": "1",
							"isShow": true,
						},
						{
							"title": "团购分类",
							"type": "picker",
							"direct": "row",
							"key": "category_id",
							"mode": "selector",
							"place": "请选择菜品分类",
							"tips": "请选择菜品分类",
							"list": this.categoryList,
							"rangeKey": "name",
							"belong": "1",
							"isShow": true,
							"isArrow": true,
						},
						{
							"title": "库存设置",
							"type": "input",
							"direct": "row",
							"key": "stock_num",
							"mode": "text",
							"place": "请输入库存数量",
							"tips": "请输入库存数量",
							"belong": "1",
							"isShow": true,
						},
						{
							"title": "售卖价格",
							"type": "input",
							"direct": "row",
							"key": "price",
							"mode": "text",
							"place": "请输入售卖价格",
							"tips": "请输入售卖价格",
							"belong": "1",
							"isShow": true,
						},
						{
							"title": "划线价格",
							"type": "input",
							"direct": "row",
							"key": "goods_yuan_price",
							"mode": "text",
							"place": "请输入划线价格",
							"tips": "请输入划线价格",
							"belong": "1",
							"isShow": true,
						},{
							"title": "限制",
							"type": "input",
							"direct": "row",
							"key": "xianzhi",
							"mode": "text",
							"place": "请输入限制",
							"tips": "请输入限制",
							"belong": "1",
							"isShow": true,
						},{
							"title": "须知",
							"type": "input",
							"direct": "row",
							"key": "xuzhi",
							"mode": "text",
							"place": "请输入须知",
							"tips": "请输入须知",
							"belong": "1",
							"isShow": true,
						},{
							"title": "保障",
							"type": "input",
							"direct": "row",
							"key": "baozhang",
							"mode": "text",
							"place": "请输入保障",
							"tips": "请输入保障",
							"belong": "1",
							"isShow": true,
						},


						
					],


					[{
						"title": "团购主图",
						"type": "upload",
						"direct": "column",
						"maxNum": "1",
						"key": "image",
						"belong": "2",
						"tips": "请上传团购主图",
						"isShow": true,
					}],
					[{
						"title": "团购轮播图",
						"type": "upload",
						"direct": "column",
						"maxNum": "9",
						"key": "images",
						"belong": "3",
						"tips": "请上传团购轮播图",
						"isShow": true,
					}],


					[{
						"title": "服务详情",
						"type": "editor",
						"key": "fuwuxiangqing",
						"belong": "6",
						"place": "请输入服务详情",
						"tips": "请输入服务详情",
						"isShow": true,
					}],
					[{
						"title": "购买须知",
						"type": "editor",
						"key": "goumaixuzhi",
						"belong": "7",
						"place": "请输入购买须知",
						"tips": "请输入购买须知",
						"isShow": true,
					}],
				

				]
			}
		},
		onLoad(options) {
			if (options.hasOwnProperty("id")) {
				this.goodsID = options.id;

				this.onGetGoodsData();
			} else {
				this.isRequestFinish = true;
			}
			this.onGetLogisticsList();
			this.onGetShopGoodsCategory();
			this.onGetGoodsCategory();
			this.onGetBrandList();
		},
		methods: {
			/**
			 * 发布
			 */
			onSubmit() {
				for (let i = 0; i < this.list.length; i++) {
					const list = this.list[i];
					if (this.$filters.judgeFormData(list, this.dataInfo)) {
						if (i === this.list.length - 1) {
							if (this.goodsID === "") {
								this.onAdd();
							} else {
								this.onSave();
							}
						}
					} else {
						return;
					}
				}
			},
			onAdd() {
				this.$throttle(() => {
					let obj = {
						...this.dataInfo
					};
					AddGoods(obj).then(res => {
						this.$st.success("添加成功", () => {
							uni.navigateBack();
						})
					})
				})
			},
			/**
			 * 保存
			 */
			onSave() {
				this.$throttle(() => {
					let obj = {
						...this.dataInfo
					};
					obj.id = this.goodsID;
					EditGoods(obj).then(res => {
						this.$st.success("保存成功", () => {
							uni.navigateBack();
						})
					})
				})
			},

			/**
			 * 获取运费模版
			 */
			onGetLogisticsList() {
				GetLogisticsList({
					type: 1
				}).then(res => {
					this.LogisticsList = res.data;
				})
			},
			/**
			 * 获取店铺内部分类
			 */
			onGetShopGoodsCategory() {
				GetShopGoodsCategory().then(res => {
					this.shopCategoryList = res.data;
				})
			},
			/**
			 * 获取平台的分类
			 */
			onGetGoodsCategory() {
				GetGoodsCategory({
					type: 1
				}).then(res => {
					this.categoryList = res.data;
				})
			},
			/**
			 * 获取品牌列表
			 */
			onGetBrandList() {
				GetBrandList().then(res => {
					this.brandList = res.data;
				})
			},
			/**
			 * 获取商品详情
			 */
			onGetGoodsData() {
				GetGoodsDetailis(this.goodsID).then(res => {
					Object.keys(this.dataInfo).forEach(key => {
						if (key === "images") {
							this.dataInfo.images = res.data.images.join(",");
						} else if (key === "spu") {
							var spuList = res.data.spu;
							for (let i = 0; i < spuList.length; i++) {
								const item = spuList[i];
								var nameList = [];
								item.skuitem.forEach(skuItem => {
									nameList.push(skuItem.name);
								})
								spuList[i].name = nameList.join(" ");
							}
							this.dataInfo.spu = spuList;
						} else {
							this.dataInfo[key] = res.data[key];
						}
					})

					this.isRequestFinish = true;
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.wrap {
		width: 100%;
		padding: 25rpx;
		box-sizing: border-box;
		overflow: hidden;

		.wrap-content {
			width: 100%;
			overflow: hidden;
			display: flex;
			flex-direction: column;
			gap: 20rpx;

			.wrap-white {
				width: 100%;
				background-color: white;
				border-radius: 20rpx;
				overflow: hidden;
				padding: 0 30rpx;
				box-sizing: border-box;
			}
		}

		.sure-btn {
			margin-top: 50rpx;
			width: 100%;
			height: 82rpx;
			line-height: 82rpx;
			text-align: center;
			border-radius: 50rpx;
			color: white;
			overflow: hidden;
			background-color: #84BD00;
			font-size: 30rpx;
		}
	}
</style>